﻿using System;
namespace Kis.Xroad.Eu {
    
    
    [global::Xtee.Core.Attributes.XmlTypeAttribute(Name="SequenceArrayComplexType", Namespace="http://kis.x-road.eu/", Target=typeof(SequenceArrayComplexType))]
    public class SequenceArrayComplexTypeSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
        
        protected SequenceArrayComplexType clazz;
        
        public SequenceArrayComplexTypeSerializer(SequenceArrayComplexType clazz) {
            this.clazz = clazz;
        }
        
        public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXmlAttributes(Writer);
        }
        
        public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
            base.WriteXml(Writer);
            Writer.WriteStartElement("ComplexReferenced");
            if (clazz.ComplexReferenced!=null) {
                if (((clazz.ComplexReferenced == null) 
                            || (clazz.ComplexReferenced.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.ComplexReferenced != null) 
                            && (x < clazz.ComplexReferenced.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.ComplexReferenced[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaComplexReferenced = this.GetMetaData(clazz.ComplexReferenced[x]);
                        if (ShouldWriteTypeAttribute(typeof(global::Kis.Xroad.Eu.Employee), clazz.ComplexReferenced[x].GetType())) {
                            Writer.WriteTypeAttribute(metaComplexReferenced.TypeQualifiedName.Namespace, metaComplexReferenced.TypeQualifiedName.Name);
                        }
                        var complexReferencedSerializer = metaComplexReferenced.NewSerializer(clazz.ComplexReferenced[x]);
                        complexReferencedSerializer.WriteXmlAttributes(Writer);
                        complexReferencedSerializer.WriteXml(Writer);
                    }
                    else {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ComplexReferenced");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ComplexReferenced");
            }
            Writer.WriteEndElement();
            Writer.WriteStartElement("ComplexInline");
            if (clazz.ComplexInline!=null) {
                if (((clazz.ComplexInline == null) 
                            || (clazz.ComplexInline.Count < 1m))) {
                    throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                }
                for (int x = 0; ((clazz.ComplexInline != null) 
                            && (x < clazz.ComplexInline.Count)); x = (x + 1)) {
                    Writer.WriteStartElement("item");
                    if (clazz.ComplexInline[x]!=null) {
                        global::Xtee.Core.SerializationMetadata metaComplexInline = this.GetMetaData(clazz.ComplexInline[x]);
                        if (ShouldWriteTypeAttribute(typeof(SequenceArrayComplexType.ComplexInlineType), clazz.ComplexInline[x].GetType())) {
                            Writer.WriteTypeAttribute(metaComplexInline.TypeQualifiedName.Namespace, metaComplexInline.TypeQualifiedName.Name);
                        }
                        var complexInlineSerializer = metaComplexInline.NewSerializer(clazz.ComplexInline[x]);
                        complexInlineSerializer.WriteXmlAttributes(Writer);
                        complexInlineSerializer.WriteXml(Writer);
                    }
                    else {
                        Writer.WriteAttributeString("xsi", "nil", null, "1");
                    }
                    Writer.WriteEndElement();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : ComplexInline");
            }
            Writer.WriteEndElement();
        }
        
        public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXmlAttributes(Reader);
        }
        
        public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
            base.ReadXml(Reader);
            if (Reader.LocalName=="ComplexReferenced") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayComplexType>ComplexReferenced");
                }
                clazz.ComplexReferenced = new System.Collections.Generic.List<global::Kis.Xroad.Eu.Employee>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ComplexReferenced");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if (Reader.IsElementNil()) {
                                throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>SequenceArrayComplexType>ComplexReferenced>item");
                            }
                            global::Xtee.Core.SerializationMetadata metaComplexReferenced = this.GetMetaData<global::Kis.Xroad.Eu.Employee>(Reader, "Employee", "http://kis.x-road.eu/");
                            global::Kis.Xroad.Eu.Employee item = metaComplexReferenced.NewEntity<global::Kis.Xroad.Eu.Employee>();
                            clazz.ComplexReferenced.Add(item);
                            if ((Reader.IsEmptyElement == false)) {
                                var complexReferencedSerializer = metaComplexReferenced.NewSerializer(item);
                                complexReferencedSerializer.ReadXmlAttributes(Reader);
                                Reader.ReadStartElement("item");
                                complexReferencedSerializer.ReadXml(Reader);
                                Reader.ReadEndElement();
                            }
                            else {
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayComplexType>ComplexReferenced>item", Reader.LocalName);
                        }
                    }
                    if ((clazz.ComplexReferenced.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayComplexType>ComplexReferenced", Reader.LocalName);
            }
            if (Reader.LocalName=="ComplexInline") {
                if (Reader.IsElementNil()) {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>SequenceArrayComplexType>ComplexInline");
                }
                clazz.ComplexInline = new System.Collections.Generic.List<SequenceArrayComplexType.ComplexInlineType>();
                if ((Reader.IsEmptyElement == false)) {
                    Reader.ReadStartElement("ComplexInline");
                    for (; ((Reader.NodeType != System.Xml.XmlNodeType.EndElement) 
                                && (Reader.LocalName == "item")); ) {
                        if (Reader.LocalName=="item") {
                            if ((Reader.IsElementNil() == false)) {
                                global::Xtee.Core.SerializationMetadata metaComplexInline = this.GetMetaData<SequenceArrayComplexType.ComplexInlineType>(Reader, ">>>SequenceArrayComplexType>ComplexInline>item", "http://kis.x-road.eu/");
                                SequenceArrayComplexType.ComplexInlineType item = metaComplexInline.NewEntity<SequenceArrayComplexType.ComplexInlineType>();
                                clazz.ComplexInline.Add(item);
                                if ((Reader.IsEmptyElement == false)) {
                                    var complexInlineSerializer = metaComplexInline.NewSerializer(item);
                                    complexInlineSerializer.ReadXmlAttributes(Reader);
                                    Reader.ReadStartElement("item");
                                    complexInlineSerializer.ReadXml(Reader);
                                    Reader.ReadEndElement();
                                }
                                else {
                                    Reader.Skip();
                                }
                            }
                            else {
                                clazz.ComplexInline.Add(null);
                                Reader.Skip();
                            }
                        }
                        else {
                            throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>SequenceArrayComplexType>ComplexInline>item", Reader.LocalName);
                        }
                    }
                    if ((clazz.ComplexInline.Count < 1m)) {
                        throw new global::Xtee.Core.Exceptions.MinOccurrenceException(1m, "item");
                    }
                    Reader.ReadEndElement();
                }
                else {
                    Reader.Skip();
                }
            }
            else {
                throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>SequenceArrayComplexType>ComplexInline", Reader.LocalName);
            }
        }
        
        [global::Xtee.Core.Attributes.XmlTypeAttribute(Name=">>>SequenceArrayComplexType>ComplexInline>item", Namespace="http://kis.x-road.eu/", Target=typeof(SequenceArrayComplexType.ComplexInlineType))]
        public class ComplexInlineTypeSerializer : global::Xtee.Core.Types.AbstractXteeSerializable {
            
            protected SequenceArrayComplexType.ComplexInlineType clazz;
            
            public ComplexInlineTypeSerializer(SequenceArrayComplexType.ComplexInlineType clazz) {
                this.clazz = clazz;
            }
            
            public override void WriteXmlAttributes(global::Xtee.Core.IXteeXmlWriter Writer) {
                base.WriteXmlAttributes(Writer);
            }
            
            public override void WriteXml(global::Xtee.Core.IXteeXmlWriter Writer) {
                base.WriteXml(Writer);
                Writer.WriteStartElement("Country");
                if (clazz.Country!=null) {
                    Writer.WriteString(new global::Xtee.Core.Schema.XsdString(((string)(clazz.Country))).ToString());
                }
                else {
                    throw new global::Xtee.Core.Exceptions.NillableNotAllowedException("Nullable is not allowed for element : Country");
                }
                Writer.WriteEndElement();
                Writer.WriteStartElement("City");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.City))).ToString());
                Writer.WriteEndElement();
                Writer.WriteStartElement("Street");
                Writer.WriteString(new global::Xtee.Core.Schema.XsdInt(((int)(clazz.Street))).ToString());
                Writer.WriteEndElement();
            }
            
            public override void ReadXmlAttributes(global::Xtee.Core.IXteeXmlReader Reader) {
                base.ReadXmlAttributes(Reader);
            }
            
            public override void ReadXml(global::Xtee.Core.IXteeXmlReader Reader) {
                base.ReadXml(Reader);
                if (Reader.LocalName=="Country") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>SequenceArrayComplexType>ComplexInline>item>Country");
                    }
                    clazz.Country = new global::Xtee.Core.Schema.XsdString(Reader.ReadElementString("Country")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>SequenceArrayComplexType>ComplexInline>item>Country", Reader.LocalName);
                }
                if (Reader.LocalName=="City") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>SequenceArrayComplexType>ComplexInline>item>City");
                    }
                    clazz.City = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("City")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>SequenceArrayComplexType>ComplexInline>item>City", Reader.LocalName);
                }
                if (Reader.LocalName=="Street") {
                    if (Reader.IsElementNil()) {
                        throw new global::Xtee.Core.Exceptions.NillableNotAllowedException(">>>>SequenceArrayComplexType>ComplexInline>item>Street");
                    }
                    clazz.Street = new global::Xtee.Core.Schema.XsdInt(Reader.ReadElementString("Street")).Value;
                }
                else {
                    throw new global::Xtee.Core.Exceptions.UnExpectedElementException(">>>>SequenceArrayComplexType>ComplexInline>item>Street", Reader.LocalName);
                }
            }
        }
    }
}
